package com.yunwrite.playerlibrary.customplayer.videolist;
import java.util.ArrayList;
import java.util.List;

/**
 * 歌单管理类，负责歌单的增删改查、消费等，使用list集合，按照index顺序播放
 * @author liuping
 */
public class VideoListManager {
    /**
     * 节目单的集合，将会顺序播放该节目单
     */
    private ArrayList<VideoInfo> videoInfos = new ArrayList<>( );
    /**
     * 歌单管理类的单例对象
     */
    private static VideoListManager videoListManager;
    private List<VideoListObserver> observerList=new ArrayList<>();
    /**
     * 不允许new，使用单例模式
     */
    private VideoListManager() {
    }
    public static VideoListManager getInstance() {
        if (videoListManager == null) {
            videoListManager = new VideoListManager();
        }
        return videoListManager;
    }
    /**
     * 获取歌单列表
     *
     * @return
     */
    public ArrayList<VideoInfo> getVideoInfos() {
        return videoInfos;
    }

    /**
     * 清空并重新设置歌单
     *
     * @param videoInfos
     */
    public void setVideoInfos(ArrayList<VideoInfo> videoInfos) {
        if (videoInfos != null) {
            this.videoInfos.clear( );
            this.videoInfos.addAll(videoInfos);
        }
        observerVideolist( );
    }

    /**
     * 插入歌单至指定位置
     *
     * @param index
     * @param videoInfos
     */
    public void insertVIdeoinfos(int index, ArrayList<VideoInfo> videoInfos) {
        if (index >= this.videoInfos.size( ) || index < 0) {
            new Exception("插入歌单位置越界，节目最大索引为" + (this.videoInfos.size( ) - 1) + ",而插入索引为" + index);
        }
        this.videoInfos.addAll(index, videoInfos);
        observerVideolist( );
    }

    /**
     * 插入歌单末尾
     *
     * @param videoInfos
     */
    public void insertVIdeoinfos(ArrayList<VideoInfo> videoInfos) {
        this.videoInfos.addAll(videoInfos);
        observerVideolist( );
    }

    /**
     * 插入单曲至指定位置
     *
     * @param index
     * @param videoInfo
     */
    public void insertVIdeoinfo(int index, VideoInfo videoInfo) {
        if (index >= this.videoInfos.size( ) || index < 0) {
            new Exception("插入歌单位置越界，节目最大索引为" + (this.videoInfos.size( ) - 1) + ",而插入索引为" + index);
        }
        this.videoInfos.add(index, videoInfo);
        observerVideolist( );
    }

    /**
     * 插入单曲至末尾
     *
     * @param videoInfo
     */
    public void insertVIdeoinfo(VideoInfo videoInfo) {
        this.videoInfos.add(videoInfo);
        observerVideolist( );
    }

    /**
     * 清空歌单
     */
    public void clear() {
        videoInfos.clear( );
        observerVideolist( );
    }
    /**
     * 通知观察者
     */
    private void observerVideolist() {
        if(null!=observerList&&observerList.size()>0){
            for (VideoListObserver observer : observerList) {
                observer.update(videoInfos);
            }
        }
    }

    /**
     * 设置歌单观察者
     *
     * @param observer
     */
    public void setObserver(VideoListObserver observer) {
        observerList.add(observer);
    }

    /**
     * 取消观察者
     *
     * @param observer
     */
    public void cancleObserver(VideoListObserver observer) {
        if (observerList.contains(observer)) {
            observerList.remove(observer);
        }
        observer = null;
    }

    /**
     * 从列表中取出一个节目，预加载
     */
    public VideoInfo prepare(){
        VideoInfo videoInfo=null;
        if(videoInfos.size()>0){
             videoInfo = videoInfos.get(0);
            videoInfos.remove(0);
        }


        return videoInfo;
    }

    /**
     * 该节目已经开始播放，将该节目从列表中移除，
     */
    public void start(){
    }


}
